package br.foton.la.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public final class DateStringUtil {

	public static final String DEFAULT_PATTERN = "dd/MM/yyyy HH:mm";

	public static String format(Date date, String pattern) {
		DateFormat format = new SimpleDateFormat(pattern);

		return format.format(date);
	}
	
	public static String format(long date, String pattern){
		DateFormat format = new SimpleDateFormat(pattern);

		return format.format(date);
	}

	public static String calcularDiferencaHoraString(String horaInicial,
			String horaFinal) {
		String[] horaI = horaInicial.split(":");
		String[] horaF = horaFinal.split(":");

		int horaIInt = Integer.parseInt(horaI[0]);
		int minutosIInt = Integer.parseInt(horaI[1]);
		int horaFInt = Integer.parseInt(horaF[0]);
		int minutosFInt = Integer.parseInt(horaF[1]);
		int resultMinutos;
		int resultHoras;

		if ((resultMinutos = minutosFInt - minutosIInt) < 0) {
			horaFInt--;
			resultMinutos += 60;
		}

		if ((resultHoras = horaFInt - horaIInt) < 0)
			resultHoras += 24;

		return String.format("%02d:%02d", resultHoras, resultMinutos);
	}

	public static String calcularSomatorioHorasString(List<String> horas) {
		int horaSoma = 0;
		int minutosSoma = 0;

		for (String hora : horas) {
			String[] partes = hora.split(":");
			horaSoma += Integer.parseInt(partes[0]);
			minutosSoma += Integer.parseInt(partes[1]);
		}

		int resto = minutosSoma % 60;
		horaSoma += (int) minutosSoma / 60;
		minutosSoma = resto;

		return String.format("%02d:%02d", horaSoma, minutosSoma);
	}

	public static Date criaDataDeString(String data) throws ParseException {
		DateFormat format = new SimpleDateFormat(DEFAULT_PATTERN + " HH:mm");
		
		return format.parse(data);
	}
	
	public static Calendar dateToCalendar(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	public static String getHorasString(Date date) {
		return format(date, "HH:mm");
	}
	
	public static String getHorasString(long date) {
		return format(date, "HH:mm");
	}

	public static boolean validateFormattedHourString(String hour) {
		String[] parts = hour.split(":");
		int hora = Integer.parseInt(parts[0].trim());
		int minuto = Integer.parseInt(parts[1].trim());

		if (hora < 0 || hora > 23)
			return false;

		if (minuto < 0 || minuto > 59)
			return false;

		return true;
	}

	public static boolean isToday(Date date) {
		String today = format(new Date(), DEFAULT_PATTERN);
		String otherDate = format(date, DEFAULT_PATTERN);

		return today.equals(otherDate);
	}
	
	public static String getTimeSystem(){
		String data = new SimpleDateFormat( "HH:mm:ss" ).format( new Date( System  
			      .currentTimeMillis() ) ) ;
		return data;
	}
	
	public static int[] getTimeFromString(String hora){
		int []array = new int[2];
		String []partes = hora.split(":");
		array[0] = Integer.parseInt(partes[0]);
		array[1] = Integer.parseInt(partes[1]);
		return array;
	}
}

